package com.neusoft.bizcore.notice.notification.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.notice.notification.dto.NoticeTemplateAttachmentDTO;
import com.neusoft.bizcore.notice.notification.model.NoticeTemplateAttachment;
import com.neusoft.bizcore.notice.notification.repository.NoticeTemplateAttachmentRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class NoticeTemplateAttachmentConverter
        extends AbstractConverter<NoticeTemplateAttachment, NoticeTemplateAttachmentDTO> {

    @Autowired
    private NoticeTemplateAttachmentRepository noticeTemplateAttachmentRepository;

    @Override
    public NoticeTemplateAttachment toModel(final NoticeTemplateAttachmentDTO dto) {
        final NoticeTemplateAttachment model;
        if (dto.isNew()) {
            model = new NoticeTemplateAttachment();
        } else {
            model = this.noticeTemplateAttachmentRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setTemplate(dto.getTemplate());
        model.setUpdateTime(new Date());
        return model;
    }

    @Override
    public NoticeTemplateAttachmentDTO toDTO(final NoticeTemplateAttachment model, final boolean forListView) {
        final NoticeTemplateAttachmentDTO dto = new NoticeTemplateAttachmentDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setTemplate(model.getTemplate());
        return dto;
    }

}
